<?php
class loginAction extends security_normal {
	
	function action(){
		$miceSession = new session();
		if($miceSession->get('mu__userid')){
			MessageBox('您已成功登录',MICE_URL.'index.php/user','','自动关闭');
			exit;
		}

		global $conn;
		if($_POST){
			if($_POST['checkcode']!=$miceSession->get('checkcode')){
				MessageBox('验证码输入错误','back');
				exit;
			}

			if($_POST['loginname']==''){
				MessageBox('请填写登录名','back');
				exit;
			}

			if($_POST['password']==''){
				MessageBox('密码不能为空','back');
				exit;
			}

			$loginsuccess = 0;
			$loginname = strtolower($_POST['loginname']);
			if($loginname){
				$user=new model_User();
				$user->setSubsection("WHERE loginname='".$loginname."'");
				$result=$user->doSelectOne();

				if(!$result){
					MessageBox('登录失败',MICE_URL,'用户名、密码或验证码错误','跳转到登录页面');
					exit;
					$loginsuccess = 0;
				}

				$pwd = sha1($result->getSalt().$_POST['password']);		

				//判断密码，验证码，
				if($pwd == $result->getPassword()){

					$group=new model_Group();
					$group->setSubsection('WHERE group_id='.$result->getGroupId());
					$groups=$group->doSelectOne();

					//判断到期时间和状态
					if(($result->getStopdate()!=0 and $result->getStopdate()<time()) or $result->getStatus()!=0){
						MessageBox('错误信息',MICE_URL,'您的帐号已经到期或被禁用');
						exit;
						$loginsuccess = 0;
					}else{
						$miceSession->set('mu__loginname', $result->getLoginname());
						$miceSession->set('mu__userid', $result->getUserId());
						$miceSession->set('mu__groupid', $result->getGroupId());
						$miceSession->set('mu__grouplevel', $groups->getLevel());
						$miceSession->set('groupmenu', $groups->getMenu());
						$miceSession->set('groupmodule', $groups->getModule());
						$user->setLastlogindate(time());
						$user->setLastloginip(GetRealIp());
						$user->doUpdate();
						MessageBox('登录成功','back','');
						exit;
						
						$loginsuccess = 1;
					}
				}
			}
			
			if($loginsuccess == 0){
				MessageBox('登录失败','back','用户名、密码或验证码错误,请重试','自动关闭');
				exit;
			}
		}

		$smarty = new Smarty();
		$smarty->load_filter('pre','micePreCompileIndex');
		$smarty->assign("name",$name);
		$smarty->assign("checkcode",MICE_URL.'code.php');

		$filename = new getTPLFileName(__CLASS__);
		$smarty->display($filename->TPLFileName);
	}

}
?>